package com.fire.social.controller;

import com.fire.social.config.Result;
import com.fire.social.config.Results;
import com.fire.social.entity.node.Content;
import com.fire.social.entity.node.User;
import com.fire.social.service.ContentService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

@Controller
public class ContentController {

    @Autowired
    private ContentService contentService;

    @PostMapping("/content/create")
    @ResponseBody
    public Result createContent(
            @RequestParam("account") String account, @RequestParam("publisher") String publisher,
            @RequestParam("publisherImg") String publisherImg, @RequestParam("title") String title,
            @RequestParam("content") String content, @RequestParam("related_hobby") String relatedHobby,
            @RequestParam("hobbyid") Long hobbyId, @RequestParam("imgurl") String imgUrl) {
        contentService.createContent(account, publisher, publisherImg, title, content,
                relatedHobby, hobbyId, imgUrl);
        if (content != null) {
            return Results.success(content);
        }
        return Results.failure();
    }

    @GetMapping("/content/publish")
    public String publishShare(HttpServletRequest request, Map<String, Object> map) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        if (user == null)
            return "login";
        map.put("user", user);
        map.put("index", "发现动态");
        map.put("title", "发布动态");
        return "addshare";
    }

    @GetMapping("/content/friend")
    public String findContentByFriend(HttpServletRequest request, Map<String, Object> map) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        if (user == null)
            return "login";
        contentService.findFriendContent(user, map);
        return "sharelist";
    }

    @GetMapping("/content/recommend")
    public String findContentByHobby(HttpServletRequest request, Map<String, Object> map) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        if (user == null)
            return "login";
        contentService.findContentByHobby(user, map);
        return "sharelist";
    }
}
